const Axios = require('axios').default
const https = require('https')
const runtimeLogger = require('./log4js')()

function Request() {
}

/**
 * Axios 发 GET 请求
 * @param url url
 * @param headers headers
 * @param ssl 服务端证书 cert/key/passphrase
 */
Request.get = function get(url, headers, ssl = {}) {
  runtimeLogger.info(`begin request ${url}`)
  console.log(`${new Date()} begin request ${url}`)

  Axios.interceptors.request.use(config => {
    return config
  })

  return Axios.get(url, {
    headers: Object.assign({
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    }, headers),
    httpsAgent: new https.Agent(Object.assign({ rejectUnauthorized: false }, ssl))
  }).then(resp => {
    runtimeLogger.info(`end request ${url}, status: ${resp.status}`)
    console.log(`${new Date()} end request ${url}, status: ${resp.status}`)
    if (resp.status === 200) {
      return { data: resp.data }
    }
  }).catch(err => {
    runtimeLogger.error(`end request ${url} error. ${err}`)
    console.error(`${new Date()} end request ${url} error. ${err}`)
  })
}

Request.post = function(url, data, headers, ssl = {}) {
  runtimeLogger.info(`begin request ${url}; data: ${JSON.stringify(data)}`)
  console.log(`${new Date()} begin request ${url}; data: ${JSON.stringify(data)}`)

  Axios.interceptors.request.use(config => {
    return config
  })

  return Axios.post(url, data, {
    headers: Object.assign({
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    }, headers),
    httpsAgent: new https.Agent(Object.assign({ rejectUnauthorized: false }, ssl))
  }).then(resp => {
    runtimeLogger.info(`end request ${url}, status: ${resp.status}`)
    console.log(`${new Date()} end request ${url}`)
    if (resp.status === 200) {
      return { data: resp.data }
    }
  }).catch(err => {
    runtimeLogger.error(`end request ${url} error. ${err}`)
    console.error(`${new Date()} end request ${url}, data: ${data}, error: ${err}, error.data: ${err && err.response && JSON.stringify(err.response.data)}`)
  })
}

Request.del = function(url, headers, ssl) {
  runtimeLogger.info(`begin request ${url}`)
  console.log(`${new Date()} begin request ${url}`)

  Axios.interceptors.request.use(config => {
    return config
  })

  return Axios.delete(url, {
    headers: Object.assign({
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    }, headers),
    httpsAgent: new https.Agent(Object.assign({ rejectUnauthorized: false }, ssl))
  }).then(resp => {
    runtimeLogger.info(`end request ${url}, status: ${resp.status}`)
    console.log(`${new Date()} end request ${url}, status: ${resp.status}`)
    if (resp.status === 200) {
      return { data: resp.data }
    }
  }).catch(err => {
    runtimeLogger.error(`end request ${url} error. ${err}`)
    console.error(`${new Date()} end request ${url} error. ${err}; Error.response: ${err && err.response && JSON.stringify(err.response.data)}`)
  })
}

module.exports = Request
